package com.hospital.ui.adapter;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hospital.R;
import com.hospital.utils.LogUtils;
import com.hospital.utils.MainUtils;

public class MainMenuExpandableListAdapter extends BaseExpandableListAdapter {
	private static final String TAG = "MainMenuExpandableListAdapter";
	private Context mContext;
	private static String[] groups;
	private static String[][] children;
	private LayoutInflater mInflater;
	private Map<Integer, View> mGroupCache;
	private Map<String, View> mChildCache;

	public MainMenuExpandableListAdapter(Context context) {
		super();
		init(context);
	}

	private void init(Context context) {
		mGroupCache = new HashMap<Integer, View>();
		mChildCache = new HashMap<String, View>();

		mContext = context;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		groups = mContext.getResources().getStringArray(R.array.groups);
		children = new String[][] {
				mContext.getResources().getStringArray(R.array.patient_mg),
				mContext.getResources().getStringArray(R.array.yizhu_mg),
				mContext.getResources().getStringArray(R.array.huli_info),
				mContext.getResources().getStringArray(R.array.other_query),
				mContext.getResources().getStringArray(R.array.system_report),
				mContext.getResources().getStringArray(R.array.system_settings) };
	}

	public Object getChild(int groupPosition, int childPosition) {
		return children[groupPosition][childPosition];
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return children[groupPosition].length;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Log.i(TAG, "getChildView groupPosition = " + groupPosition
				+ " childPosition = " + childPosition);
		View v = getChildViewFromCache(groupPosition, childPosition);
		ChildHolder holder;
		if (v == null) {
			v = mInflater.inflate(R.layout.left_sub_menu_item, null);
			holder = new ChildHolder();
			holder.image = (ImageView) v.findViewById(R.id.item_icon);
			holder.text = (TextView) v.findViewById(R.id.item_label);
			holder.text.setText(getChild(groupPosition, childPosition)
					.toString());
			v.setTag(holder);
			setChildViewToCache(groupPosition, childPosition, v);
		}
		return v;
	}

	public View getChildViewFromCache(int groupPosition, int childPosition) {
		if (mChildCache != null) {
			String key = String.valueOf(groupPosition) + childPosition;
			if (mChildCache.containsKey(key)) {
				Log.i(TAG, "getGroupViewFromCache");
				return mChildCache.get(key);
			}
		}
		return null;
	}

	private void setChildViewToCache(int groupPosition, int childPosition,
			View view) {
		if (mChildCache != null) {
			String key = MainUtils.genKey(groupPosition, childPosition);
			mChildCache.put(key, view);
		}
	}

	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	public int getGroupCount() {
		return groups.length;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		Log.i(TAG, "getGroupView groupPosition = " + groupPosition);
		View v = getGroupViewFromCache(groupPosition);
		GroupHolder holder;
		if (v == null) {
			v = mInflater.inflate(R.layout.left_group_menu_item, null);
			holder = new GroupHolder();
			holder.text = (TextView) v.findViewById(R.id.item_label);
			holder.text.setText(getGroup(groupPosition).toString());
			v.setTag(holder);
			setGroupViewToCache(groupPosition, v);
		}
		return v;
	}

	private void setGroupViewToCache(int groupPosition, View v) {
		if (mGroupCache != null) {
			Integer position = new Integer(groupPosition);
			mGroupCache.put(position, v);
		}
	}

	public View getGroupViewFromCache(int groupPosition) {
		if (mGroupCache != null) {
			Integer position = new Integer(groupPosition);
			if (mGroupCache.containsKey(position)) {
				Log.i(TAG, "getGroupViewFromCache");
				return mGroupCache.get(position);
			}
		}
		return null;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	public static class GroupHolder {
		public TextView text;
	}

	public static class ChildHolder {
		public ImageView image;
		public TextView text;
	}

}